package com.solution.admin.auth.security;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.alibaba.dubbo.container.Main;
import com.solution.admin.auth.constants.AuthConstants;
import com.solution.common.config.PublicConfigUtil;
import com.solution.common.utils.StringUtils;

/**
 * 自定义authc过滤器
 * @author llp
 * @date 2017/5/20 2:34
 */
@Service("solutionFormAuthenticationFilter")
public class SolutionFormAuthenticationFilter extends FormAuthenticationFilter {

	/**
	 * 判断是否登录
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected boolean isAccessAllowed(ServletRequest request,ServletResponse response, Object mappedValue) {
		HttpServletRequest req = (HttpServletRequest)request;
		String requestPath = req.getServletPath();
		
		System.out.println(req.getHeader("host")+"==="+req.getHeader("referer"));
		boolean flag = super.isAccessAllowed(request, response, mappedValue);
		if("/index.jsp".equals(requestPath)){
			flag = true;
		}
		if(!flag){
			ServletContext sc = req.getSession().getServletContext();
	        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
	        // 标记存储在redis中
	        RedisTemplate template = (RedisTemplate)cxt.getBean("redisTemplate");
	        BoundValueOperations options = template.boundValueOps("LOGIN_TERMINAL_"+req.getSession().getId().substring(12));
			String userAgent = req.getHeader("user-agent").toLowerCase();
			if(userAgent.indexOf("micromessenger")>-1){
				//微信客户端
				options.set(AuthConstants.LOGIN_TERMINAL_TYPE.WX.getCode(),30,TimeUnit.MINUTES);
			}else{
				// 判断是pc前端还是后台
				if(requestPath.startsWith("/pc")){
					options.set(AuthConstants.LOGIN_TERMINAL_TYPE.PC.getCode(),30,TimeUnit.MINUTES);
				}else{
					options.set(AuthConstants.LOGIN_TERMINAL_TYPE.BACKEND.getCode(),30,TimeUnit.MINUTES);
				}
			}
		}
		return flag;
	}

	@Override
	protected boolean onLoginSuccess(AuthenticationToken token,
			Subject subject, ServletRequest request, ServletResponse response)
			throws Exception {
		return super.onLoginSuccess(token, subject, request, response);
	}

	@Override
	protected boolean onLoginFailure(AuthenticationToken token,
			AuthenticationException e, ServletRequest request,
			ServletResponse response) {
		return super.onLoginFailure(token, e, request, response);
	}

	public static void main(String[] args) {
		System.out.println(UUID.randomUUID().toString().replace("-", ""));
	}
	
}